﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetDon.WebControls;
using NetDon.eBoss.Data.CollectionClasses;
using NetDon.eBoss.Data.EntityClasses;
using NetDon.eBoss.Data.HelperClasses;
using NetDon.eBoss.Portal.Common;

namespace NetDon.OA.Portal.HtmlTool
{
    public partial class StyleEdit : NetDonPage
    {



        #region Properties
        protected int CategoryRuleId
        {
            get
            {
                return Parameters.GetInt32("CategoryRuleId");
            }
        }
        protected string BatchId
        {
            get
            {
                return Parameters.GetString("BatchId");
            }
        }


        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {


            if (!IsPostBack)
            {

                CancelButton = btnReturn;
                BindDropDownLists();
                if (!string.IsNullOrEmpty(BatchId))
                {
                    var collections = new HtmltoolstyleCollection();
                    collections.GetMulti(HtmltoolstyleFields.Batchid == BatchId);
                    foreach (var collection in collections)
                    {
                        SetStyles(collection);
                    }

                }

            }
        }

        private void SetStyles(HtmltoolstyleEntity collection)
        {
            if (collection.Sitecode == (int)eBaySites.US)
            {
                txtUSRule.Text = collection.Style;
            }
            if (collection.Sitecode == (int)eBaySites.UK)
            {
                txtUKRule.Text = collection.Style;
            }
            if (collection.Sitecode == (int)eBaySites.AU)
            {
                txtAURule.Text = collection.Style;
            }
            if (collection.Sitecode == (int)eBaySites.DE)
            {
                txtDERule.Text = collection.Style;
            }
            if (collection.Sitecode == (int)eBaySites.FR)
            {
                txtFRRule.Text = collection.Style;
            }
            if (collection.Sitecode == (int)eBaySites.Site1)
            {
                txtSite1Rule.Text = collection.Style;
            }
            if (collection.Sitecode == (int)eBaySites.Site2)
            {
                txtSite2Rule.Text = collection.Style;
            }
            if (collection.Sitecode == (int)eBaySites.Site3)
            {
                txtSite3Rule.Text = collection.Style;
            }
            if (collection.Sitecode == (int)eBaySites.Site4)
            {
                txtSite4Rule.Text = collection.Style;
            }
        }



        private void BindDropDownLists()
        {

        }




        private bool save()
        {
            HtmltoolstyleEntity entity = null;
         
            if (string.IsNullOrEmpty(BatchId))
            {
                var batchId = Guid.NewGuid().ToString();
                //add
                SaveStyles(new HtmltoolstyleEntity(){Sitecode =(int)eBaySites.US}, batchId);
                SaveStyles(new HtmltoolstyleEntity() { Sitecode = (int)eBaySites.AU }, batchId);
                SaveStyles(new HtmltoolstyleEntity() { Sitecode = (int)eBaySites.UK }, batchId);
                SaveStyles(new HtmltoolstyleEntity() { Sitecode = (int)eBaySites.DE }, batchId);
                SaveStyles(new HtmltoolstyleEntity() { Sitecode = (int)eBaySites.FR }, batchId);
                SaveStyles(new HtmltoolstyleEntity() { Sitecode = (int)eBaySites.IT }, batchId);
                SaveStyles(new HtmltoolstyleEntity() { Sitecode = (int)eBaySites.Site1 }, batchId);
                SaveStyles(new HtmltoolstyleEntity() { Sitecode = (int)eBaySites.Site2 }, batchId);
                SaveStyles(new HtmltoolstyleEntity() { Sitecode = (int)eBaySites.Site3 }, batchId);
                SaveStyles(new HtmltoolstyleEntity() { Sitecode = (int)eBaySites.Site4 }, batchId);
            }
            else
            {
                var collections = new HtmltoolstyleCollection();
                collections.GetMulti(HtmltoolstyleFields.Categoryruleid == CategoryRuleId & HtmltoolstyleFields.Batchid ==BatchId);
                //modify
                foreach (var collection in collections)
                {
                    SaveStyles(collection, collection.Batchid);
                }
            }
          
            return true;
        }


        private void SaveStyles(HtmltoolstyleEntity entity, string batchId)
        {
            if (entity.Sitecode == (int)eBaySites.US)
            {
                entity.Style = txtUSRule.Text;
            }
            if (entity.Sitecode == (int)eBaySites.UK)
            {
                entity.Style = txtUKRule.Text;
            }
            if (entity.Sitecode == (int)eBaySites.AU)
            {
                entity.Style = txtAURule.Text;
            }
            if (entity.Sitecode == (int)eBaySites.DE)
            {
                entity.Style = txtDERule.Text;
            }
            if (entity.Sitecode == (int)eBaySites.FR)
            {
                entity.Style = txtFRRule.Text;
            }
            if (entity.Sitecode == (int)eBaySites.IT)
            {
                entity.Style = txtITRule.Text;
            }
            if (entity.Sitecode == (int)eBaySites.Site1)
            {
                entity.Style = txtSite1Rule.Text;
            }
            if (entity.Sitecode == (int)eBaySites.Site2)
            {
                entity.Style = txtSite2Rule.Text;
            }
            if (entity.Sitecode == (int)eBaySites.Site3)
            {
                entity.Style = txtSite3Rule.Text;
            }
            if (entity.Sitecode == (int)eBaySites.Site4)
            {
                entity.Style = txtSite4Rule.Text;
            }
            entity.Categoryruleid = CategoryRuleId;
            entity.Batchid = batchId;
            entity.Save();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            if (save())
            {
                Success("保存成功!");
                CloseWindow(true);
            }



        }




    }

}